package com.mz361.modules.system.repository;


import com.mz361.modules.system.domain.PayOrder;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author niceale
 * @ date 2024/05/07
 */
public interface PayOrderRepository extends BaseRepository<PayOrder, Long> {

    PayOrder findByOrderId(String orderId);

    @Query(value = "select sum(o.total_fee) from std_pay_order o where o.result_status = 'SUCCESS' and o.source_key=?1 and o.status=1", nativeQuery = true)
    Long getTotalPerformancebySourcekey(String sourceKey);

    @Query(value = "select sum(o.total_fee) from std_pay_order o where o.result_status = 'SUCCESS'   and o.status=1", nativeQuery = true)
    Long getTotalPerformance();

    @Query(value = "select count(o.id) from std_pay_order o where o.result_status = 'SUCCESS' and o.source_key=?1   and o.status=1", nativeQuery = true)
    Long getCountOrderbySourcekey(String sourceKey);

    @Query(value = "select count(o.id) from std_pay_order o where o.result_status = 'SUCCESS'  and o.status=1", nativeQuery = true)
    Long getCountOrder();

    @Query(value = "select sum(o.total_fee) from std_pay_order o where o.result_status = 'SUCCESS' and o.invite_code=?1  and o.status=1", nativeQuery = true)
    Long getTotalPerformancebyInviteCode(String inviteCode);

    @Query(value = "select count(o.id) from std_pay_order o where o.result_status = 'SUCCESS' and o.invite_code=?1  and o.status=1", nativeQuery = true)
    Long getCountOrderbyInviteCode(String inviteCode);

    @Query("from PayOrder o where o.memeberId=?1 and o.resultStatus=?2 and o.invoiceStatus=?3 and o.status<>3")
    List<PayOrder> findAllByMemeberIdAndResultStatusIsAndInvoiceStatus(Long memeberId, String resultStatus, String invoiceStatus);

    @Query("from PayOrder o where o.memeberId=?1 and o.orderType='1' and (o.resultStatus='SUCCESS' or o.resultStatus='CLOSED')")
    List<PayOrder> findAllByMemeberId(Long memeberId);

    @Query("from PayOrder o where o.memeberId=?1 and o.paidPackageTag=?2 and (o.resultStatus='SUCCESS' or o.resultStatus='CLOSED')")
    PayOrder findByMemeberIdAndPaidPackageTag(Long memeberId, String paidPackageTag);

    @Query("from PayOrder o where o.memeberId=?1 and o.paidPackageTag=?2 and o.resultStatus !='SUCCESS' and o.resultStatus !='CLOSED'")
    PayOrder findByMemeberIdAndPaidPackageTagNotSuccess(Long memeberId, String paidPackageTag);

    @Query("from PayOrder o where o.companyId=?1 and o.paidPackageTag=?2 and (o.resultStatus='SUCCESS' or o.resultStatus='CLOSED')")
    PayOrder findByCompanyAndPaidPackageTag(Long companyId, String paidPackageTag);

    @Query("from PayOrder o where o.companyId=?1 and o.paidPackageTag=?2 and o.resultStatus!='SUCCESS' and o.resultStatus!='CLOSED'")
    PayOrder findByCompanyAndPaidPackageTagNotSuccess(Long companyId, String paidPackageTag);

}